package com.tms.quartz.mail.impl;

import com.tms.domain.Tool;
import com.tms.quartz.mail.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * 邮件服务实现类
 *
 * @author ozj
 * @date 2020年6月30日
 */
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    TemplateEngine templateEngine;
    private final static String sender = "1145790078@qq.com";

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(sender);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }


    @Override
    public void sendMailWithFile(String to, String subject, String content, String filePath) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(to);
            helper.setText(content);
            FileSystemResource resource = new FileSystemResource(new File(filePath));
            String fileName = resource.getFilename();
            assert fileName != null;
            helper.addAttachment(fileName, resource);

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        javaMailSender.send(message);
    }

    /**
     * 夹具预警邮件
     *
     * @param to      邮件接收者
     * @param subject 主题
     * @param content 内容
     * @param tool    夹具
     */
    @Override
    public void sendMailUseHtml(String to, String subject, String content, Tool tool) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(to);
            helper.setText(content);
            helper.setSentDate(new Date());
            Context context = new Context();
            context.setVariable("username", "javaboy");
            context.setVariable("num", "000001");
            context.setVariable("salary", "99999");
            String process = templateEngine.process("mail.html", context);
            helper.setText(process, true);

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        javaMailSender.send(message);
    }
}
